<?php

namespace app\admin\controller;

use app\service\BooksService;

/**
 * 书单管理
 * @author   csb 
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Books extends Common {

    /**
     * 构造方法
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct() {
        // 调用父类前置方法
        parent::__construct();

        // 登录校验
        $this->IsLogin();

        // 权限校验
        $this->IsPower();
    }

    /**
     * [Index 书单列表]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
    public function Index() {

        // 参数
        $params = input();
        // 总数
        $total = BooksService::BooksTotal();

        // 分页
        $number = MyC('admin_page_number', 10, true);
        $page_params = array(
            'number' => $number,
            'total' => $total,
            'page' => isset($params['page']) ? intval($params['page']) : 1,
            'url' => MyUrl('admin/books/index'),
        );
        $page = new \base\Page($page_params);

        // 获取数据列表
        $data_params = [
            'm' => $page->GetPageStarNumber(),
            'n' => $number
        ];
        $ret = BooksService::BooksList($data_params);
        $this->assign('params', $params);
        $this->assign('page_html', $page->GetPageHtml());
        $this->assign('data_list', $ret['data']);
      
        return $this->fetch();
    }
   
     /**
     * [Index 添加书单数据]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
    public function addbooksGoods() {
            // 参数
        $params = input();
       return BooksService::addBookList($params); 
       
    }
    
    /**
     * [Index 书单列表]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
    public function booksGoods() {
            // 参数
        $params = input();
        $ret = BooksService::getBooksGoodsList($params['id']);   //传入书单id   
        $this->assign('books_goods', $ret['data']);
         $this->assign('params', $params);
        return $this->fetch();
    }

    /**
     * [SaveInfo 书单添加/编辑页面]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-14T21:37:02+0800
     */
    public function SaveInfo() {
        // 参数
        $params = input();
        // 商品信息
        $data = [];
        if (!empty($params['id'])) {
            $data_params = [
                'where' => ['id' => $params['id']],
            ];
            $ret = BooksService::BooksList($data_params);
            if (empty($ret['data'][0])) {
                return $this->error('书单信息不存在', MyUrl('admin/books/index'));
            }
            $data = $ret['data'][0];
        }
        // 数据
        $this->assign('data', $data);
        $this->assign('params', $params);
        return $this->fetch();
    }

    
         /**
	 * [StatusHomeRecommended 是否推荐状态更新]
	  
	 */
	public function StatusRecommended()
	{
		// 是否ajax
		if(!IS_AJAX)
		{
			return $this->error('非法访问');
		}
		// 开始操作
		$params = input('post.');
		$params['admin'] = $this->admin;
		$params['field'] = 'is_recommended';
		return BooksService::BooksStatusUpdate($params);
	}
        
        
           /**
	 * [StatusHomeRecommended 是否推荐状态更新]
	  
	 */
	public function statusUpdate()
	{
		// 是否ajax
		if(!IS_AJAX)
		{
                    return $this->error('非法访问');
		}
		// 开始操作
		$params = input('post.');
		$params['admin'] = $this->admin;
		$params['field'] = 'status';
		return BooksService::BooksStatusUpdate($params);
	}
        
    /**
     * [Save 商品添加/编辑]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-14T21:37:02+0800
     */
    public function Save() {
        
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }

        // 开始操作
        $params = input('post.');
        $params['admin'] = $this->admin;
        return BooksService::BooksSave($params);
    }

    /**
     * [Delete  书单删除]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-15T11:03:30+0800
     */
    public function Delete() {
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }

        // 开始操作
        $params = input('post.');
        $params['admin'] = $this->admin;
        return BooksService::BooksListDelete($params);
    }

}

?>